<template>
  <el-card class="box-card">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="width: 300px">
      <el-breadcrumb-item style="font-size: 16px">首页</el-breadcrumb-item>
      <el-breadcrumb-item style="font-size: 16px">学校管理</el-breadcrumb-item>
      <el-breadcrumb-item style="font-size: 16px"
        >热门大学管理</el-breadcrumb-item
      >
    </el-breadcrumb>

    <el-table
      :data="userData"
      border
      style="
        width: 1001px;
        position: relative;
        text-align: center;
        margin-left: 30px;
      "
      :header-cell-style="headClass"
    >
      <el-table-column label="序号" width="100" type="index" align="center">
      </el-table-column>
      <el-table-column label="院校名称" width="350" prop="name" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="院校热度" width="350" prop="heat" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.heat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button
            style="float: left"
            v-if="showEdit"
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="showEditDialog(scope.row.name)"
          ></el-button>
          <!-- <el-button
            style="float: left"
            v-if="showRemove"
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="removeUniByName(scope.row.name)"
          ></el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="修改信息"
      :visible.sync="editDialogVisible"
      width="400px"
      @close="editDialogClosed"
    >
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef">
        <el-form-item label="院校名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="院校热度" prop="heat">
          <el-input
            v-model.number="editForm.heat"
            autocomplete="off"
            maxlength="12"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import qs from "qs";
import throttling from "@/public/throttling.js";
export default {
  data() {
    var validateName = (rule, value, callback) => {
      // 只能输入汉字
      let reg = /^[\u4e00-\u9fa5]+$/;
      if (!value) {
        return callback(new Error("院校名称不能为空!"));
      } else {
        if (!reg.test(value)) {
          console.log("输入不合法");
          callback(new Error("请输入汉字！"));
        } else if (value.search("大学") === -1) {
          callback(new Error("请输入正确的大学名称!"));
        } else {
          console.log("输入合法");
          console.log(typeof value);
          callback();
        }
      }
    };
    var validateHeat = (rule, value, callback) => {
      if (!Number.isInteger(value)) {
        callback(new Error("请正确填写热度值！该值为数字！"));
      } else {
        callback();
      }
    };
    return {
      // 显示隐藏
      showEdit: true,
      showRemove: true,
      // 控制修改院校
      editDialogVisible: false,
      // 修改用户的表单数据
      editForm: {
        fname: "",
        name: "",
        heat: "",
      },
      // 属性
      editFormRules: {
        name: [
          {
            validator: validateName,
            trigger: "blur",
          },
        ],
        heat: [
          {
            validator: validateHeat,
            trigger: "blur",
          },
        ],
      },
      //初始分页条件
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
        count: 0,
      },
      // 保存请求回来的用户列表数据
      userData: [
        {
          category: null,
          code: 0,
          feature: null,
          heat: 2789,
          is211: null,
          is985: null,
          logo: null,
          name: "华南理工大学",
          province: null,
          ts: null,
          type: null,
          unvid: 0,
        },
      ],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    // 表格表头
    headClass() {
      //表头居中显示
      return "color:rgba(255,101,0);font-weight:bold;font-size:16px";
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
    },
    handleCurrentChange: throttling(function (page) {
      this.queryInfo.pagenum = page;
      console.log("跳转了");
      // 重新按照pagenum发送请求 请求最新的数据
      this.getUniList_Token();
    }, 1000),
    // 获取院校数据
    getUniList_Token() {
      const token = window.sessionStorage.getItem("tokenvalue");
      this.$axios.defaults.headers.common["satoken"] = token;
      this.$axios.get("/back/hotUniversities").then((res) => {
        console.log(res);
        if (res.data.status === 666) {
          this.$message.error("请先登录");
          this.$router.replace("/login");
        } else {
          this.$message.success("获取热门院校列表成功！");
          this.userData = res.data;
        }
      });
    },
    // 是否显示编辑弹窗
    showEditDialog(name) {
      //将获取到的数据保存到数据editForm中
      this.editForm.fname = name;
      //显示弹出窗
      this.editDialogVisible = true;
    },
    cancel() {
      this.editDialogVisible = false;
      this.$message("取消修改");
    },
    editDialogClosed() {
      // 对话框关闭之后，重置表单
      this.$refs.editFormRef.resetFields();
    },
    editUser: throttling(function () {
      // 用户点击修改表单中的确定按钮之后，验证表单数据
      this.$refs.editFormRef.validate((valid) => {
        if (!valid) return;
        else {
          this.$axios
            .put(
              "/back/hotUniversity",
              qs.stringify({
                fName: this.editForm.fname,
                name: this.editForm.name,
                heat: this.editForm.heat,
              })
            )
            .then((res) => {
              console.log(res);
              //修改成功的提示
              this.$message.success("修改热门院校成功");
              //关闭对话框
              this.editDialogVisible = false;
              //重新请求最新的数据
              this.getUniList_Token();
            });
        }
      });
    }, 1000),
  },
  created() {
    this.$axios.get("/back/hotUniversity/updateButton").then((res) => {
      if (res.data.code === 500) {
        this.showEdit = false;
      }
    });
    this.getUniList_Token();
  },
};
</script>

<style scoped>
.box-card {
  position: relative;
  margin: 22px auto;
  min-height: 550px;
}
.text {
  font-size: 14px;
}

.item {
  padding: 18px 0;
}

.search {
  margin-top: 20px;
  margin-left: 40px;
  /* position: absolute; */
  /* right: 80px;
  top: 6px; */
}

.el-table {
  margin-top: 20px;
}

.el-dialog .el-dialog__body {
  padding: 10px 20px;
}

.el-form-item {
  margin-bottom: 10px;
}

.pagination {
  /* border: 1px solid red; */
  /* position: absolute; */
  /* bottom: 20px; */
  width: 412px;
  margin: 5px auto;
  /* height: 30px; */
  /* left: 50%; */
  /* margin-left: -206px; */
}
</style>